package com.jzt.jk.open.api.manage.application.controller;

import com.jzt.jk.open.api.manage.application.assembler.CommonAssembler;
import com.jzt.jk.open.api.manage.contracts.dto.request.ManagerLoginReq;
import com.jzt.jk.open.api.manage.contracts.dto.response.ManagerLoginRes;
import com.jzt.jk.open.api.manage.contracts.dto.response.MenuRes;
import com.jzt.jk.open.api.manage.domain.sys.bo.ManagerLoginReqBo;
import com.jzt.jk.open.api.manage.domain.sys.bo.ManagerLoginResBo;
import com.jzt.jk.open.api.manage.domain.sys.service.LoginService;
import com.jzt.jk.open.api.manage.infrastructure.common.model.UserInfo;
import com.jzt.jk.open.api.manage.infrastructure.repository.EnumConstant;
import com.jzt.jk.open.api.manage.infrastructure.repository.po.Menu;
import com.jzt.jk.open.api.manage.infrastructure.util.tree.BuildTreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：lizw <br/>
 * 创建时间：2021/01/21 11:31 <br/>
 */
@RestController
@RequestMapping("/api")
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 运营端登录
     */
    @PostMapping("/manager_login")
    public ManagerLoginRes managerLogin(HttpServletResponse response, @RequestBody @Validated ManagerLoginReq req) {
        ManagerLoginResBo resBO = loginService.managerLogin(response, CommonAssembler.toBo(req, ManagerLoginReqBo.class));
        return CommonAssembler.toDto(resBO, ManagerLoginRes.class);
    }

    /**
     * 运营端菜单
     */
    @PostMapping("/manager_menus")
    public List<MenuRes> getManagerMenus() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUid("001");
        List<Menu> menuList = loginService.getMenus(userInfo.getUid(), EnumConstant.Scope_1);
        List<MenuRes> menuResList = CommonAssembler.toBoList(menuList, MenuRes.class);
        return BuildTreeUtils.buildTree(menuResList);
    }
}
